﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.Helpers;

namespace Fayshine.VA
{
    public partial class LoginScreen : UserControl
    {
        public LoginScreen()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(LoginScreen_Loaded);
        }

        void LoginScreen_Loaded(object sender, RoutedEventArgs e)
        {
            FayshineManager.Instance.GetUserInfoFinished += new GetUserInfoFinishedEventHandler(Instance_GetUserInfoFinished);
        }

        private void Instance_GetUserInfoFinished(user user)
        {
            this.txtUserName.IsEnabled = true;
            this.txtPassword.IsEnabled = true;
            this.buttonPlayPause.IsEnabled = true;

            if (user == null)
            {
                lblErrorInfo.Foreground = new SolidColorBrush(Colors.Red);
                lblErrorInfo.Text = "用户名或者密码错误，请检查后登录";
                lblErrorInfo.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                lblErrorInfo.Visibility = System.Windows.Visibility.Collapsed;
                SessionManager.Session["UserInfo"] = user;
                ContentControl contentControl = this.Parent as ContentControl;
                if (contentControl != null)
                {
                    contentControl.Content = new MainPage();
                }
            }
        }
        private void PasswordBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (ValidLogin())
                {
                    this.txtUserName.IsEnabled = false;
                    this.txtPassword.IsEnabled = false;
                    this.buttonPlayPause.IsEnabled = false;
                    this.lblErrorInfo.Text = "正在登录...";
                    this.lblErrorInfo.Foreground = new SolidColorBrush(Colors.Green);
                    this.lblErrorInfo.Visibility = System.Windows.Visibility.Visible;

                    string userName = txtUserName.Text.Trim();
                    string pwd = txtPassword.Password.Trim();
                    FayshineManager.Instance.GetLoginUser(userName,pwd);
                }
            }
        }

        private void buttonPlayPause_Click(object sender, RoutedEventArgs e)
        {
            if (ValidLogin())
            {
                this.txtUserName.IsEnabled = false;
                this.txtPassword.IsEnabled = false;
                this.buttonPlayPause.IsEnabled = false;
                this.lblErrorInfo.Text = "正在登录...";
                this.lblErrorInfo.Foreground = new SolidColorBrush(Colors.Green);
                this.lblErrorInfo.Visibility = System.Windows.Visibility.Visible;

                string userName = txtUserName.Text.Trim();
                string pwd = txtPassword.Password.Trim();
                FayshineManager.Instance.GetLoginUser(userName,pwd);
            }
        }

        private bool ValidLogin()
        {
            bool success = false;
            string userName = txtUserName.Text.Trim();
            string pwd = txtPassword.Password.Trim();
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(pwd))
            {
                success = true;
            }
            else
            {
                lblErrorInfo.Text = "请输入正确的用户名或者密码";
                lblErrorInfo.Visibility = System.Windows.Visibility.Visible;
            }

            return success;
        }
    }
}
